package com.imc.api.order.repository;

import com.imc.api.order.pojo.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/*
 * 类描述：OrderRepository
 * 开发者：tanweiping
 * 创建日期：2018/10/28 21:23
 */
@Repository
public interface IWxMpOrderRepository extends JpaRepository <Order, Integer>, JpaSpecificationExecutor <Order> {

    /*
     * YT  2018/10/28 21:27
     * 方法描述：通过交易号码用户ID查询所有的订单信息
     */
    Page <Order> findAllByUserIdAndStatusAndTradeNoNotNull(
            String userId,
            Integer status,
            Pageable pageable);

    /*
     * YT  2018/10/28 21:25
     * 方法描述：通过交易号码查询所有的订单信息
     */
    Page <Order> findAllByUserIdAndTradeNoNotNull(
            String userId,
            Pageable pageable);

    /*
     * YT  2018/10/28 21:25
     * 方法描述：通过交易号查找订单信息
     */
    Order findAllByTradeNo(String tradeNo);

    /*
     * YT  2018/10/28 21:24
     * 方法描述：根据商品IDS和用户ID获取数量
     */
    @Query(
            value = "SELECT count(mOrder.id) FROM t_order_detail detail\n" +
                    " INNER JOIN t_order mOrder ON detail.orderId = mOrder.id\n" +
                    " WHERE detail.goodsId IN (:goodsIds) AND mOrder.userId=:userId",
            nativeQuery = true
    )
    int queryCountForSku(@Param("goodsIds") List <String> goodsIds, @Param("userId") String userId);
}